package cn.com.bluemoon.daps.system.controller.api;


import cn.com.bluemoon.daps.api.sys.RemoteSystemTagsService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiSystem;
import cn.com.bluemoon.daps.common.datascope.IgnorePermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.system.entity.DapSystemTag;
import cn.com.bluemoon.daps.system.service.DapSystemTagService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Api(tags = {"FeignClient服务-标签api接口"})
@RequestMapping(DapsApiSystem.API_SYS_TAG)
@IgnorePermission(name = "服务注册-标签")
@RestController
@Slf4j
public class DapTagsApi implements RemoteSystemTagsService {

    @Resource
    DapSystemTagService dapSystemTagService;

    /**
     * 根据模型id获得关联的系统标签
     * @return
     */
    @GetMapping("/list")
    public ResultBean<List<DapSystemTag>> getTags(@RequestParam("modelId") String modelId,
                                           @RequestParam("status") Integer status){

        List<DapSystemTag> listById = dapSystemTagService.getListById(modelId, status);
        return ResultBean.ok(listById);
    }

    /**
     * 获取系统标签
     * @return 获取系统标签
     */
    @GetMapping("/")
    public ResultBean<List<DapSystemTag>> list(){
        List<DapSystemTag> list = dapSystemTagService.list(dapSystemTagService.bmLambdaQuery());
        return ResultBean.ok(list);
    }

    /**
     * 根据标签名称获取标签
     *
     * @param name 根据标签名称获取标签
     * @return 根据标签名称获取标签
     */
    @GetMapping("/get")
    public ResultBean<Optional<DapSystemTag>> getByName(@RequestParam("name") String name){
        String nameT = StrUtil.trim(name);
        LambdaQueryWrapper<DapSystemTag> eq = dapSystemTagService.bmLambdaQuery().eq(DapSystemTag::getName, nameT).last(" limit 1");
        return ResultBean.ok(Optional.ofNullable(dapSystemTagService.getOne(eq)));
    }
}
